/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sjtu.bean;

import com.sjtu.database.DataBase;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author p w
 */
public class CommentBean {

    private int id;
    private int userId;
    private int bookId;
    private String content;
    private Date createDate;
    private int Score;

    public CommentBean(int id, int userId, int bookId, String content, Date createDate, int Score) {
        this.id = id;
        this.userId = userId;
        this.bookId = bookId;
        this.content = content;
        this.createDate = createDate;
        this.Score = Score;
    }

    public int getScore() {
        return Score;
    }

    public void setScore(int Score) {
        this.Score = Score;
    }

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    private static CommentBean setComment(ResultSet rs) {
        try {
            if (rs == null || !rs.next()) {
                return null;
            }
            return new CommentBean(rs.getInt("id"), rs.getInt("userId"),
                    rs.getInt("bookId"), rs.getString("comment"),
                    rs.getDate("date"), rs.getInt("score"));
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static CommentBean getComment(String content) {
        try {
            DataBase database = DataBase.getInstance();
            ResultSet rs = database.query("select * from comment where comment = '" + content + "';");
            CommentBean user = setComment(rs);
            rs.close();
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean addComment(int userId, int bookId, String comment, int score) {
        try {
            DataBase database = DataBase.getInstance();

            Date dateNow = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String createDate = dateFormat.format(dateNow);
            String sql = "insert into comment (userId,bookId,comment,date,score) values (" + userId + "," + bookId + ",'" + comment + "','" +
                    createDate + "'," + score + ");";
//            System.out.println(sql);
            database.update(sql);
            ResultSet rs = database.query("select popularity from book where id = " + bookId + ";");
            rs.next();
            double popular = rs.getDouble("popularity") + 0.3;
            return database.update("update book set popularity = " + popular + "where id = " + bookId + ";");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static ArrayList<CommentBean> getComment(int bookId, String userName) {
        ArrayList<CommentBean> comments = new ArrayList<CommentBean>();
        try {
            DataBase database = DataBase.getInstance();
            ResultSet rs;
            CommentBean comm;
            if (userName.equals("")) {
                rs = database.query("select * from comment where bookId = " + bookId + ";");
                while ((comm = CommentBean.setComment(rs)) != null) {
                    comments.add(comm);
                }
            } else {
                rs = database.query("select id from user where username = '" + userName + "';");
                rs.next();
                int userid = rs.getInt("id");
                rs = database.query("select * from comment where bookId = " + bookId + " and userId = " + userid + ";");
                while ((comm = CommentBean.setComment(rs)) != null) {
                    comments.add(comm);
                }
            }
            return comments;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
